<?php
namespace Admin\Controller;

use Think\Controller;
use Common\Util\Page;
use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;

class CommentsController extends AdminbaseController {
    public function index(){
    	$CommentsModel = M('comments');
    	
    	$mobile = I('get.mobile');
    	$orderNumber = I('get.order_number');
    	$content = I('get.keyword');
    	$beauticianId = I('get.beautician_id');
    	$beautyId = I('get.beauty_id');
    	
    	$where = array('c.parentid' => 0);
    	$dataSelectWhere = array();
    	
    	if ($beauticianId) {
    		$where ['o.beautician_id'] = $beauticianId;
    		$dataSelectWhere ['o.beautician_id'] = $beauticianId;
    	}
    	
    	if ($beautyId) {
    		$where ['o.beauty_id'] = $beautyId;
    		$dataSelectWhere ['o.beauty_id'] = $beautyId;
    	}
    	
    	if ($mobile) {
    		$where ['u.mobile'] = $mobile;
    		$dataSelectWhere ['u.mobile'] = $mobile;
    	}
    	
    	if ($orderNumber) {
    		$where ['o.order_number'] = $orderNumber;
    		$dataSelectWhere ['o.order_number'] = $orderNumber;
    	}
    	
    	if ($content) {
    		$where ['c.content'] = array('like', '%'.$content.'%');
    		$dataSelectWhere ['c.content'] = array('like', '%'.$content.'%');
    	}
    	
   		$count = $CommentsModel
   			->join('c left join '.C('DB_PREFIX').'user as u on c.uid=u.id')
   			->join('left join '.C('DB_PREFIX').'order as o on c.post_id=o.id')
   		->where($where)->count();
   		
   		$page = $this->page($count, 20);
   		
    	$list = $CommentsModel->field(array('c.*', 'u.username', 'u.mobile', 'b.name' => 'beautician_name', 'o.order_number'))->where(array('c.parentid' => 0))
    		->join('c left join '.C('DB_PREFIX').'user as u on c.uid=u.id')
    		->join('left join '.C('DB_PREFIX').'beautician as b on c.to_uid=b.id')
    		->join('left join '.C('DB_PREFIX').'order as o on c.post_id=o.id')
    		->order('c.createtime desc')
    		->where($dataSelectWhere)
    		->limit($page->firstRow.','.$page->listRows)->select();
    	
    	$this->assign('list', $list);
    	$this->assign('Page', $page->show('Admin'));
    	$this->display();
    }
    
    public function detail() {
    	$id = I('get.id');
    	
    	$where = array('c.id' => $id, 'c.status' => 1);
    	 
    	$CommentsModel = M('comments');
    	
    	$detail = $CommentsModel->field(array('c.*', 'u.username', 'u.mobile', 'b.name' => 'beautician_name', 'o.order_number'))->where(array('c.parentid' => 0))
    		->join('c left join '.C('DB_PREFIX').'user as u on c.uid=u.id')
    		->join('left join '.C('DB_PREFIX').'beautician as b on c.to_uid=b.id')
    		->join('left join '.C('DB_PREFIX').'order as o on c.post_id=o.id')
    		->where($where)->find();
    	
    	$commentPhotos = M('comment_photo')->where(array('comment_id' => $id))->select();
    	
    	$this->assign('item', $detail);
    	$this->assign('photos', $commentPhotos);
    	
    	$this->display();
    }
    
    public function delete() {
    	$CommentsModel = M('comments');
    	$CommentsModel->startTrans();
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$where = array('id' => $id);
    		
    		$result = $CommentsModel->where($where)->save(array('status' => 0));
    		if ($result) {
    			$comment = $CommentsModel->where(array('id' => $id))->find();
    			\Think\Hook::listen('delete_comment', $comment);

    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			
    			$CommentsModel->commit();
    			$this->success('success', U('index'));
    		} else {
    			$CommentsModel->rollback();
    			$this->error('fail');
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $CommentsModel->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',', $ids));
    			$result = $CommentsModel->where($where)->save(array('status' => 0));
    		}
    		
    		if (!$result) {
    			$CommentsModel->rollback();
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		
    		foreach ($ids as $commentId) {
    			$comment = $CommentsModel->where(array('id' => $commentId))->find();
    			\Think\Hook::listen('delete_comment', $comment);
    			
    			$operation = array('operation_object_id' => $commentId);
    			\Think\Hook::listen('operation', $operation);
    		}
    		$CommentsModel->commit();
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
}